<?php

namespace App\Repository;


use App\Model\SmsCodeTable;
use App\Repository\Sys\JsonRespose;

class BaseService
{
    static public function useCode($phone, $code)
    {

        // 测试验证码
        if (config('app.debug')) {
            if ($code == '123456') {
                return JsonRespose::jsonArray(1);
            }
        }
        
        $valid_time = date('Y-m-d H:i:s', time() - 300); //5分钟内有效
        $res = SmsCodeTable::ORM()
            ->where('sms_code_phone', $phone)
            ->where('created_at', '>=', $valid_time)
            ->where('sms_code_status', '=', 1)
            ->where('sms_code_val', '=', $code)
            ->first();

        if (!$res) {
            return JsonRespose::jsonArray(0, [], '验证码错误');
        } else {
            $res = SmsCodeTable::ORM()
                ->where('sms_code_id', $res->sms_code_id)->update([
                    'sms_code_status' => 2
                ]);

            if (!$res) {
                return JsonRespose::jsonArray(0, [], '使用失败');
            }
        }
        return JsonRespose::jsonArray(1);

    }

}